<?php

namespace App\Services\User;

use App\Models\BlockedUser;
use App\Models\User;

class BlockUserService
{
    public static function ban(User $user, $attributes = [])
    {
        $user->update($attributes);
        $criteria = [
            'user_id' => $user->id,
        ];
        $basic = [
            'reason' => $attributes['remark'] ?? '-',
            'action' => request()->path(),
            'payload' => json_encode([
                'url' => request()->fullUrl(),
                'header' => request()->headers,
                'post' => request()->all(),
            ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT),
            'version' => request()->header('APPCODE'),
        ];
        $blockUser = BlockedUser::query()->where($criteria)->first();
        if ($blockUser) {
            $blockUser->update(array_merge([
                'times' => $blockUser->times + 1,
            ], $basic));
        } else {
            BlockedUser::query()->create(array_merge([
                'user_id' => $user->id,
                'times' => 1,
            ], $basic));
        }
    }
}
